package edu.uta.shorthorn.news.ui;


import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import edu.uta.shorthorn.news.constants.*;
import edu.uta.shorthorn.news.model.*;
import edu.uta.shorthorn.news.parser.*;
import edu.uta.shorthorn.utility.Image;


public class Opinion extends Activity {
	/** Called when the activity is first created. */
	ListView headlines;
	ArrayList<String> array = new ArrayList<String>(); 
	LinearLayout layout;
	private List<Message> messages;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.headlines);
		
		// Show the Ad at the top - ad image is pulled from a URL
		// Ad image when clicked takes to the Ad page
		ImageView image = (ImageView) findViewById(R.id.ad_image);
		image.setImageBitmap(Image.getView(Ad.AD4));
		image.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) 
	        {
	            String url = Ad.AD4_page;  
	            Intent i = new Intent(Intent.ACTION_VIEW);  
	            i.setData(Uri.parse(url));  
	            startActivity(i);     
	        }
		});

		try
		{
			FeedParser parser = FeedParserFactory.getParser(ParserType.ANDROID_SAX, Category.OPINION);
			messages = parser.parse();	  

			headlines = (ListView) findViewById(R.id.headlines);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.listItem, array);
			for (Message msg : messages)
			{
				adapter.add(msg.getTitle());
			}
			headlines.setAdapter(adapter);	 
		} 
		catch (Throwable t)
		{
			Log.e("ShorthornNews",t.getMessage(),t);
		}
		headlines.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) { 
				Intent intent = new Intent().setClass(Opinion.this, NewsContent.class);
				String title = messages.get(position).getTitle();
				String content = messages.get(position).getDescription();
				String pubDate = messages.get(position).getDate();
				String author = messages.get(position).getAuthor();
				String link = messages.get(position).getLink().toString();
				
				// images in the content String need to use relative locations to be displayed on WebView 
				// Since the RSS feed returns all images with base URL(http://www.shorthorn.com)
				// We need to remove them to retain ONLY relative URLs: so...
				content.replaceAll("src=\"http://www.theshorthorn.com", "src=\"");
				//content.replaceAll("\\{jathumbnail off\\}", "");
				// NOTE: the base URL is passed in the NewsContent.java (see webview.loadDataWithBaseURL)
				intent.putExtra("title", title);
				intent.putExtra("content", content);
				intent.putExtra("pubDate", pubDate);
				intent.putExtra("author", author);
				intent.putExtra("link", link);
				startActivity(intent);
			}
		});
	}
}
